import json
from datetime import datetime
import logging
from datetime import datetime, timedelta
from Util.Public import *


def INFO(info_type, info):  # 输出状态信息
    log_path = get_path(os.path.abspath(__file__), 2, get_config("config", "log_path"))
    logging.basicConfig(filename=log_path, level=logging.INFO, format='%(asctime)s - %(levelname)s : %(message)s')
    # 根据不同的类型输出不同的日志
    if info_type == "INFO":
        logging.info(info)
    elif info_type == "WARNING":
        logging.warning(info)
    elif info_type == "ERROR":
        logging.error(info)
    elif info_type == "DEBUG":
        logging.debug(info)

    now_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f")
    message = f"INFO {now_time} - {info}"
    print(message)


def process_bbox(s):  # 将bbox从字符串形式转化为数组形式
    # 移除字符串中的括号
    s = s.strip("()")
    # 使用逗号分隔字符串
    parts = s.split(", ")
    # 将每个字符串转换为浮点数
    numbers = [float(part) for part in parts]
    return numbers


def update_start_time(start_time):
    json_path = 'Config/{profile}.json'.format(profile="manage")  # 获得文件相对路径
    join_path = get_path(os.path.abspath(__file__), 2, json_path)
    with open(join_path, 'r', encoding="utf-8") as file:
        config = json.load(file)

    start_time = datetime.strptime(start_time, "%Y-%m-%d")
    stop_time = datetime.now() - timedelta(days=get_config("config", "data_manage")["stop_days"])
    if start_time > stop_time:  # 如果日期大于明天，则置为明天
        start_time = stop_time

    config["data_date"]["start_time"] = start_time.strftime("%Y-%m-%d")
    with open(join_path, 'w', encoding="utf-8") as file:
        json.dump(config, file)